//
//  KABAddAddressTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/3/4.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABAddAddressTCell.h"
#import "GlobalHeader.h"
#import "KABModel.h"

@interface KABAddAddressTCell ()<UITextViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) NSArray<NSString *> *titleArray;
@end

@implementation KABAddAddressTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.textView.delegate = self;
    
    self.textField.delegate = self;
    [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - <UITextViewDelegate>
- (void)textViewDidChange:(UITextView *)textView {
    
    NSString *toBeString = textView.text;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > KABGlobalReceiverAddressLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:KABGlobalReceiverAddressLength];
            if (rangeIndex.length == 1) {
                textView.text = [toBeString substringToIndex:KABGlobalReceiverAddressLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, KABGlobalReceiverAddressLength)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    if ([self.delegate respondsToSelector:@selector(kABAddAddressTCell:textViewDidChanged:)]) {
        [self.delegate kABAddAddressTCell:self textViewDidChanged:textView];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(kABAddAddressTCell:textViewDidEndEditing:)]) {
        [self.delegate kABAddAddressTCell:self textViewDidEndEditing:textView];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField.markedTextRange == nil && textField.text.length > KABGlobalPhoneNumLength) {
        textField.text = [textField.text substringToIndex:KABGlobalPhoneNumLength];
    }
    
    if ([self.delegate respondsToSelector:@selector(kABAddAddressTCell:textFieldDidChanged:)]) {
        [self.delegate kABAddAddressTCell:self textFieldDidChanged:textField];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(kABAddAddressTCell:textFieldDidEndEditing:)]) {
        [self.delegate kABAddAddressTCell:self textFieldDidEndEditing:textField];
    }
}

- (void)configViewWithRecInfoItem:(KABReceiverInfoItem *)item index:(NSInteger)index {
    self.item = item;
    
    if (index < self.titleArray.count) {
        self.titleLabel.text = self.titleArray[index];
    }
    if (index == 0) {
        self.textField.text = item.receiver_name;
        
    } else if (index == 1) {
        self.textField.text = item.receiver_phone;
        
    } else if (index == 2) {
        NSString * str = [NSString stringWithFormat:@"%@ %@", item.province, item.city];
        if ((!item.province) || (!item.city)) {
            str = nil;
        }
        self.textField.text = str;
        
    } else if (index == 3) {
        self.textView.text = item.address;
    }
    
    self.textField.textAlignment = (index != 2) ? NSTextAlignmentLeft : NSTextAlignmentRight;
    self.textField.userInteractionEnabled = (index != 2);
    self.textField.placeholder = (index == 2) ? @"请选择" : nil;
    self.arrowImgView.hidden = (index != 2);
    self.textView.hidden = (index != 3);
    self.textField.hidden = (index == 3);
}

- (NSArray<NSString *> *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"收货人", @"联系方式", @"所在地区", @"详细地址"];
    }
    return _titleArray;
}

@end
